package com.platform.admin.enums;

import com.platform.admin.contant.BusinessConstant;
import lombok.Getter;

import javax.validation.groups.Default;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;


/**
 * 异常源枚举
 * 异常来源 1跟单宝小程序,2电子看板,3跟单宝web,4印刷系统,5PDA智能打包,6PDA智能打包系统生成
 *
 * @author: yuanlai
 * @date 2022/11/12
 */
@Getter
public enum ExceptionSourceEnum {

//    GDB_X("1", "跟单宝小程序", 0, OneGroup.class) {
//        @Override
//        public List<ExHandleResultEnum> getHandleWays() {
//            return NORMAL_HANDLES;
//        }
//    },
//    BW("2", "机长看板", 1, TwoGroup.class) {
//        @Override
//        public List<ExHandleResultEnum> getHandleWays() {
//            return NORMAL_HANDLES;
//        }
//    },
//    GDB_WEB("3", "跟单宝web", 0, ThreeGroup.class) {
//        @Override
//        public List<ExHandleResultEnum> getHandleWays() {
//            return NORMAL_HANDLES;
//        }
//    },
//    PAINTING("4", "生产排单系统", 0, FourGroup.class) {
//        @Override
//        public List<ExHandleResultEnum> getHandleWays() {
//            return Arrays.asList(ExHandleResultEnum.OFFLINE, ExHandleResultEnum.RESUME);
//        }
//    },
//    PDA("5", "PDA智能打包台", 1, FiveGroup.class) {
//        @Override
//        public List<ExHandleResultEnum> getHandleWays() {
//            return NORMAL_HANDLES;
//        }
//    },
    /**
     * 系统生成的打包异常
     **/
    PDA_SYS("6", "PDA智能打包台", 1, Default.class) {
        @Override
        public List<ExHandleResultEnum> getHandleWays() {
            return PDA_SYS_HANDLES;
        }
    };
    /**
     * 0.线下处理,1.恢复生产,2.强制完成,3.已外发,4.跳过此工序 【针对普通停止生产的和继续生产的异常】
     */
    public static final List<ExHandleResultEnum> NORMAL_HANDLES =
            Arrays.asList(ExHandleResultEnum.OFFLINE, ExHandleResultEnum.RESUME, ExHandleResultEnum.FORCE_FINISH, ExHandleResultEnum.OUTING, ExHandleResultEnum.SKIP);
    /**
     * PDA系统生成的异常：
     * 剩余：
     * 5.送客户，6.打包入库，7.直接扔掉，
     * 缺少：
     * 8.补优惠券，9.退差价，10.补印，11.无需处理
     */
    public static final List<ExHandleResultEnum> PDA_SYS_HANDLES =
            Arrays.asList(ExHandleResultEnum.TO_CUSTOMER
                    , ExHandleResultEnum.PACK_WAREHOUSE, ExHandleResultEnum.THROW_AWAY
                    , ExHandleResultEnum.COUPON, ExHandleResultEnum.REFUND_DIFF
                    , ExHandleResultEnum.REPRINT, ExHandleResultEnum.NO_NEED);
    /**
     * 有看板id
     */
//    public static List<String> HAS_MAIN_ID = Arrays.asList(BW.getCode(), GDB_WEB.getCode(), PDA.getCode(), PDA_SYS.getCode(), GDB_X.getCode());
//    public static List<String> BW_REL = Arrays.asList(BW.getCode(), PDA.getCode());
    private final String code;
    private final String name;
    /**
     * 用户类型 0 跟单宝 1 机长看板
     */
    private final Integer userType;
    private final Class<? extends Default> validGroup;

    ExceptionSourceEnum(String code, String name, Integer userType, Class<? extends Default> validGroup) {
        this.code = code;
        this.name = name;
        this.userType = userType;
        this.validGroup = validGroup;
    }

    /**
     * 是否跟单宝用户
     *
     * @param code 异常来源code
     * @return boolean
     */
    public static boolean isGdbUser(String code) {
        for (ExceptionSourceEnum sourceEnum : values()) {
            if (sourceEnum.code.equals(code) && Objects.equals(sourceEnum.userType, BusinessConstant.NUM_ZERO)) {
                return true;
            }
        }
        return false;
    }

    public static String getName(String code) {
        for (ExceptionSourceEnum sourceEnum : values()) {
            if (Objects.equals(sourceEnum.getCode(), code)) {
                return sourceEnum.getName();
            }
        }
        return null;
    }

    public static Class<? extends Default> getValidGroupClassBySource(String source) {
        for (ExceptionSourceEnum typeEnum : values()) {
            if (typeEnum.getCode().equals(source)) {
                return typeEnum.getValidGroup();
            }
        }
        return Default.class;
    }

    public static ExceptionSourceEnum getInstanceByCode(String source) {
        for (ExceptionSourceEnum typeEnum : values()) {
            if (typeEnum.getCode().equals(source)) {
                return typeEnum;
            }
        }
        return null;
    }

    /**
     * 获取异常来源对应的所有异常处理方式
     *
     * @return {@link List}<{@link ExHandleResultEnum}>
     */
    public abstract List<ExHandleResultEnum> getHandleWays();

}
